<?php
// +----------------------------------------------------------------------
// | Author: Sam
// +----------------------------------------------------------------------
namespace plugins\web;
use cmf\lib\Plugin;
use think\Db;
use think\Request;
class WebPlugin extends Plugin
{

    public $info = [
        'name'        => 'Web',
        'title'       => '站点管理',
        'description' => '站点管理，站群、多站、多语言',
        'status'      => 1,
        'author'      => 'Sam',
        'version'     => '1.1',
        'demo_url'    => '',
        'author_url'  => ''
    ];

    public $hasAdmin = 1;//插件是否有后台管理界面
    // 插件安装
    public function install()
    {
       //读取数据库配置内容
       $dbConfig = \think\Config::get('database');
		$db = Db::connect($dbConfig);
        $sql = <<<sql
        CREATE TABLE  IF NOT EXISTS `{$dbConfig['prefix']}plugin_web` (
			`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID号',
			`web_url` varchar(100) NOT NULL DEFAULT '' COMMENT '域名',
			`pc_theme` varchar(50) NOT NULL DEFAULT '' COMMENT '电脑端模板',
			`mob_theme` varchar(50) NOT NULL DEFAULT '' COMMENT '移动端模板',
			`msg` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
			`status` tinyint(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '状态;1:正常;0:禁用',
			`update_time` int(10) DEFAULT NULL COMMENT '更新时间',
			`create_time` int(10) DEFAULT NULL COMMENT '创建时间',
            PRIMARY KEY (`id`)
          ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='站点管理表';
sql;
        $db->execute($sql);
		return true;
    }
    // 插件卸载
    public function uninstall()
    {
      $dbConfig = \think\Config::get('database');
		$db = Db::connect($dbConfig);
        $sql = <<<sql
        DROP TABLE IF EXISTS `{$dbConfig['prefix']}plugin_web`;
sql;
        $db->execute($sql);
        return true;
    }
    public function switchTheme($param)
    {
		$request = Request::instance();
        $now_url = $request->host();
		$data = Db::name('plugin_web')->where(["web_url" => $now_url,"status"=>1])->find();
		if(!empty($data) && !empty($data['pc_theme'])){
			if(cmf_is_mobile() == true && !empty($data['mob_theme'])){
				$newTheme = $data['mob_theme'];
			}else{
				$newTheme = $data['pc_theme'];
			}
			return $newTheme;
		}
    }

}